#include "borrowreturnwidget.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSplitter>
#include <QTableWidget>
#include <QTextBrowser>
#include <QTextEdit>

#include <QHeaderView>

#include "ElaText.h"
#include "ElaLCDNumber.h"
#include "ElaPlainTextEdit.h"
#include "ElaLineEdit.h"
#include "ElaPushButton.h"
#include "ElaTableView.h"
#include "B_TableViewModel.h"

BorrowReturnWidget::BorrowReturnWidget(QWidget *parent)
    : QWidget{parent}
{

    initUI();
}

BorrowReturnWidget::~BorrowReturnWidget()
{

}

void BorrowReturnWidget::initUI()
{
    // ===== 主布局 =====
    QSplitter *mainSplitter = new QSplitter(Qt::Horizontal, this);

    // 左侧面板
    leftPanel = new QWidget;
    setupLeftPanel();
    mainSplitter->addWidget(leftPanel);

    // 右侧面板
    rightPanel = new QWidget;
    setupRightPanel();
    mainSplitter->addWidget(rightPanel);

    // 主窗口布局
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(mainSplitter);
    setLayout(mainLayout);

    // 初始化硬件连接
    rfid = new QSerialPort(this);
    rfid->setPortName("COM3");
    rfid->open(QIODevice::ReadWrite);

    initConnections();
}

void BorrowReturnWidget::onRfidDataReceived()
{

}

void BorrowReturnWidget::onBorrowClicked()
{

}

void BorrowReturnWidget::onReturnClicked()
{

}
// ===== 左侧面板布局 =====
void BorrowReturnWidget::setupLeftPanel()
{
    // QWidget *leftPanel = findChild<QWidget*>();
    QGridLayout *left_layout = new QGridLayout(leftPanel);

    // 读者卡号区
    // QGridLayout *left_layout = new QGridLayout();

    // QVBoxLayout *cardLayout = new QVBoxLayout;
    cardEdit = new ElaLineEdit;
    cardEdit->setPlaceholderText("等待读取读者卡...");

    ElaText *le_card = new ElaText("读者卡号:", this);
    le_card->setTextStyle(ElaTextType::Body);

    ElaPushButton *rescanBtn = new ElaPushButton("重新扫描");

    left_layout->addWidget(le_card, 0, 0);
    left_layout->addWidget(cardEdit, 0, 1);
    left_layout->addWidget(rescanBtn, 0, 2);

    // layout->addLayout(cardLayout);

    // 图书ID区
    QVBoxLayout *bookLayout = new QVBoxLayout;
    bookEdit = new ElaLineEdit;
    bookEdit->setMaximumHeight(100);

    ElaText *le_book = new ElaText("图书ID:", this);
    le_book->setTextStyle(ElaTextType::Body);

    left_layout->addWidget(le_book, 1, 0);
    left_layout->addWidget(bookEdit, 1, 1);

    // 操作按钮区
    QVBoxLayout *btnLayout = new QVBoxLayout;
    borrowBtn = new ElaPushButton("借阅");
    returnBtn = new ElaPushButton("归还");
    printBtn = new ElaPushButton("打印凭条");
    left_layout->addWidget(borrowBtn, 2, 1);
    left_layout->addWidget(returnBtn, 3, 1);
    left_layout->addWidget(printBtn, 4, 1);
    QSpacerItem *verticalSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
    // 添加垂直弹簧（方法1）
    left_layout->addItem(verticalSpacer, 5, 1, 1, 1);

    // 倒计时显示
    timerLCD = new ElaLCDNumber;
    timerLCD->setIsUseAutoClock(true);
    timerLCD->setIsTransparent(false);
    //    _lcdNumber->setAutoClockFormat("hh:mm:ss");
    timerLCD->setFixedHeight(100);
    // timerLCD->setDigitCount(2);
    // timerLCD->display(30);
    left_layout->addWidget(timerLCD, 6, 1);

    left_layout->setRowStretch(0, 1);
    left_layout->setRowStretch(1, 1);
    left_layout->setRowStretch(2, 1);
    left_layout->setRowStretch(3, 1);
    left_layout->setRowStretch(4, 1);
    left_layout->setRowStretch(5, 4);
    left_layout->setRowStretch(6, 1);
}
// ===== 右侧面板布局 =====
void BorrowReturnWidget::setupRightPanel()
{
    // QWidget *rightPanel = findChild<QWidget*>();
    QVBoxLayout *right_layout = new QVBoxLayout(rightPanel);

    // 借阅记录表
    recordTable = new ElaTableView(this);

    QFont tableHeaderFont = recordTable->horizontalHeader()->font();
    tableHeaderFont.setPixelSize(16);
    recordTable->horizontalHeader()->setFont(tableHeaderFont);  // 设置列头字体大小
    recordTable->setModel(new B_TableViewModel());
    recordTable->setSelectionBehavior(QAbstractItemView::SelectRows);   // 选中模式
    recordTable->setAlternatingRowColors(true); // 行颜色交替
    recordTable->setIconSize(QSize(38, 38));    // 设置图标大小
    recordTable->verticalHeader()->setHidden(true);  // 设置行号显示
    recordTable->horizontalHeader()->setMinimumSectionSize(60); // 设置列最小宽度
    recordTable->verticalHeader()->setMinimumSectionSize(46);   // 设置行最小宽度
    recordTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); // 设置自动调整占比

    // 系统日志
    logBrowser = new ElaPlainTextEdit;
    logBrowser->appendPlainText("<系统> 欢迎使用自助借还系统");

    right_layout->addWidget(recordTable, 3); // 3:2比例
    right_layout->addWidget(logBrowser, 2);
}

void BorrowReturnWidget::initConnections()
{
    // RFID数据接收
    // connect(rfid, &QSerialPort::readyRead, this, &BorrowReturnWidget::handleRfidData);

    // 按钮信号
    // connect(borrowBtn, &QPushButton::clicked, this, &BorrowReturnWidget::processBorrow);
    // connect(returnBtn, &QPushButton::clicked, this, &BorrowReturnWidget::processReturn);
}

